package com.library.controller;


import com.library.entity.Book;
import com.library.entity.BorrowRequest;
import com.library.service.BookService;
import com.library.service.BorrowService;
import com.library.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping
public class BorrowController {
    @Autowired
    private BorrowService borrowService;
    @Autowired
    private BookService bookService;
    @Autowired
    private UserService userService;


    /**
     * 将书籍添加到购物车。
     *
     * @param bookId  需要添加到购物车的书籍ID。
     * @param session 当前的HTTP会话，用于存储购物车数据。
     * @param model   Spring模型对象，用于向视图传递反馈消息。
     * @return 重定向到书籍列表页面，并附带操作结果消息。
     */
    @PostMapping("/borrowCart")
    public String addToCart(@RequestParam("bookId") Long bookId, HttpSession session, Model model) {
        List<Book> cart = (List<Book>) session.getAttribute("cart");
        if (cart == null) {
            cart = new ArrayList<>();
        }
        cart = borrowService.addToCart(bookId, cart);
        session.setAttribute("cart", cart);
        return "borrowCart";
    }

    @GetMapping("/deleteBorrowCart/{bookId}")
    public String deleteFromCart(@PathVariable("bookId") Long bookId, HttpSession session, Model model) {
        List<Book> cart = (List<Book>) session.getAttribute("cart");
        if (cart == null) {
            model.addAttribute("message", false);
            return "borrowCart";
        }
        cart = borrowService.deleteFromCart(bookId, cart);
        session.setAttribute("cart", cart);
        return "redirect:/borrowCart";
    }

    @PostMapping("/submitBorrowCart")
    public String submitRequest(HttpSession session, Model model, @RequestParam("days") List<Integer> days, RedirectAttributes redirectAttributes) {
        List<Book> cart = (List<Book>) session.getAttribute("cart");
        if (cart == null) {
            model.addAttribute("message", false);
            return "borrowCart";
        }
        Boolean result = borrowService.submitBorrowRequest(cart, (long) session.getAttribute("id"),days);
        model.addAttribute("message", result);
        session.setAttribute("cart", null);
        redirectAttributes.addFlashAttribute("borrowMsg", "借阅成功！");
        return "redirect:/home";
    }

    @GetMapping("/borrowRequest")
    public String borrowRequest(HttpSession session) {
        long userId=(long) session.getAttribute("id");
        List<BorrowRequest> borrowRequests=borrowService.findAllBR(userId);
        Map<Long,String> userMap=userService.getId_Name();
        Map<Long,String> bookMap=bookService.getId_BookName();
        session.setAttribute("userNames",userMap);
        session.setAttribute("bookTitles",bookMap);
        session.setAttribute("requests", borrowRequests);
        return "borrowRequests";
    }

    @GetMapping("/borrowCart")
    public String borrowCart(HttpSession session, Model model) {
        return "borrowCart";
    }

    @PostMapping("/borrowRequests/approve")
    public String approveBR(@RequestParam("id")Long id){
        borrowService.agreeBorrowRequest(id);
        return "redirect:/borrowRequest";
    }

    @PostMapping("/borrowRequests/reject")
    public String rejectBR(@RequestParam("id")Long id){
        borrowService.rejectBorrowRequest(id);
        return "redirect:/borrowRequest";
    }

    @PostMapping("/borrowRequests/return")
    public String returnBR(@RequestParam("id")Long id){
        borrowService.returnBorrowRequest(id);
        return "redirect:/borrowRequest";
    }

    @PostMapping("/borrowRequests/delete")
    public String deleteBR(@RequestParam("id")Long id){
        borrowService.deleteBorrowRequest(id);
        return "redirect:/borrowRequest";
    }

    @PostMapping("/borrowRequests/add")
    public String addBR(@RequestParam("bookId")long bookId,@RequestParam("days") List<Integer> days,
                        HttpSession session){
        Long userId = (Long) session.getAttribute("id");
        Book book=bookService.findBookById(bookId);
        List<Book> books=new ArrayList<>();
        books.add(book);
        borrowService.submitBorrowRequest(books,userId,days);
        return "redirect:/borrowRequest";
    }

}
